#!/bin/bash

#echo "DEBUG: $ 1 = Full path to the installation package the installer app is processing: " $1
#echo "DEBUG: $ 2 = Full path to the installation destination: " $2
#echo "DEBUG: $ 3 = Installation volume (mountpoint) to receive the payload: " $3
#echo "DEBUG: $ 4 = Root directory for the system: " $4

echo "preinstall: Path to installer....... $1"
echo "preinstall: Path to destination..... $2"
echo "preinstall: Path to dest volume..... $3"
echo "preinstall: Root of system folder... $4"

#############################################################################

DEST_VOL="${3}"
EFI_ROOT_DIR=$(cd "${DEST_VOL}"/EFIROOTDIR; pwd -P)
install_log="${DEST_VOL}/Clover_Install_Log.txt"
driver_dir="@DRIVER_DIR@"
driver_name="@DRIVER_NAME@"

if [[ -d "$EFI_ROOT_DIR/EFI/CLOVER/drivers/$driver_dir" ]]; then
  cd "$EFI_ROOT_DIR/EFI/CLOVER/drivers/$driver_dir"
  if [[ "$driver_name" == OcQuirks.efi ]]; then
    for driver in *.efi
    do
      low=$(echo "${driver}" | tr '[:upper:]' '[:lower:]')
      if [[ "$driver" != $driver_name ]] && \
      [[ "$low" == *aptiofix* || "$low" == *memfix*  || "$low" == *memoryfix* ]]; then
        echo "Removing ${driver_dir}/${driver} because $driver_name is installed" >> "$install_log"
        rm -f "${driver}"
      fi
    done
  else
    for driver in *.efi
    do
      low=$(echo "${driver}" | tr '[:upper:]' '[:lower:]')
      if [[ "$driver" != $driver_name ]] && \
      [[ "$low" == *aptiofix* || "$low" == *memfix*  || "$low" == *memoryfix* \
      || "$low" == *ocquirks* || "$low" == *openruntime* ]]; then
        echo "Removing ${driver_dir}/${driver} because $driver_name is installed" >> "$install_log"
        rm -f "${driver}"
      fi
    done
  fi
fi
